package com.vrp3d.web;

import com.vrp3d.common.enums.UserExceptionEnum;
import com.vrp3d.common.response.RestResponse;
import com.vrp3d.domain.dto.user.UserForChangePasswordDTO;
import com.vrp3d.domain.dto.user.UserForLoginDTO;
import com.vrp3d.domain.dto.user.UserForRecoverPasswordDTO;
import com.vrp3d.domain.dto.user.UserForRegisteredDTO;
import com.vrp3d.domain.validator.Mobile;
import com.vrp3d.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author vrp3d
 */
@Api("用户相关接口")
@Slf4j
@Validated
@RestController
public class LoginController extends AbsController {

    @Autowired
    private UserService userService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "手机号/邮箱,密码登录,返回token(全局唯一,退出登录后,之前的token失效,有效的token一定是最后登录后获得的token)")
    @ApiResponses({
          @ApiResponse(code = 21001, message = "用户不存在"),
          @ApiResponse(code = 21002, message = "登录账户密码错误")
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RestResponse login(@Validated @RequestBody UserForLoginDTO user, HttpServletRequest request, HttpServletResponse response) {
        String result = userService.login(user, request, response);
        return new RestResponse().put(result);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public RestResponse logout() {
        userService.logout();
        return new RestResponse(UserExceptionEnum.USER_EXIT.getMsg());
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "注册时,获取手机验证码,1条/分钟,5条/小时,10条/天")
    @ApiResponses({
          @ApiResponse(code = 200, message = "验证码发送成功"),
          @ApiResponse(code = 21000, message = "用户已存在(已经注册过)"),
          @ApiResponse(code = 21006, message = "手机号码格式不正确"),
          @ApiResponse(code = 21007, message = "验证码一分钟最多发送1条"),
          @ApiResponse(code = 21008, message = "验证码一小时最多发送5条"),
          @ApiResponse(code = 21009, message = "验证码一天最多发送10条")
    })
    @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, paramType = "query")
    @RequestMapping(value = "/getPhoneVerificationCodeWhenRegistering", method = RequestMethod.GET)
    public RestResponse getPhoneVerificationCodeWhenRegistering(@Mobile String mobile) {
        userService.getPhoneVerificationCodeWhenRegistering(mobile);
        return new RestResponse(UserExceptionEnum.VERIFICATION_CODE_SENT_SUCCESSFULLY.getMsg());
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "注册")
    @ApiResponses({
          @ApiResponse(code = 200, message = "用户注册成功"),
          @ApiResponse(code = 21019, message = "密码不符合规范"),
    })
    @RequestMapping(value = "/registered", method = RequestMethod.POST)
    public RestResponse registered(@Validated @RequestBody UserForRegisteredDTO user) {
        userService.registered(user);
        return new RestResponse(UserExceptionEnum.REGISTRATION_SUCCESS.getMsg());
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "手机号找回密码,发送验证码,手机验证码1条/分钟,5条/小时,10条/天")
    @ApiResponses({
          @ApiResponse(code = 200, message = "验证码发送成功"),
          @ApiResponse(code = 21013, message = "请输入正确的手机号码"),
          @ApiResponse(code = 21007, message = "验证码一分钟最多发送1条"),
          @ApiResponse(code = 21008, message = "验证码一小时最多发送5条"),
          @ApiResponse(code = 21009, message = "验证码一天最多发送10条"),
          @ApiResponse(code = 21012, message = "验证码发送成功"),
    })
    @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, paramType = "query")
    @RequestMapping(value = "/getRecoverPasswordVerificationCode", method = RequestMethod.GET)
    public RestResponse getRecoverPasswordVerificationCode(@Mobile String mobile) {
        return userService.getRecoverPasswordVerificationCode(mobile);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "找回密码_重置密码")
    @ApiResponses({
          @ApiResponse(code = 200, message = "密码修改成功"),
          @ApiResponse(code = 21001, message = "用户不存在"),
          @ApiResponse(code = 21011, message = "验证码不正确"),
          @ApiResponse(code = 21013, message = "请输入正确的手机号码"),
          @ApiResponse(code = 21017, message = "验证码已失效"),
          @ApiResponse(code = 21019, message = "输入的密码不符合规范")
    })
    @RequestMapping(value = "/recoverPassword", method = RequestMethod.POST)
    public RestResponse recoverPassword(@Validated @RequestBody UserForRecoverPasswordDTO user) {
        userService.recoverPassword(user);
        return new RestResponse(UserExceptionEnum.PASSWORD_RESET_COMPLETE.getMsg());
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "修改密码")
    @ApiResponses({
          @ApiResponse(code = 200, message = "密码修改成功"),
          @ApiResponse(code = 21021, message = "原密码错误"),
          @ApiResponse(code = 21018, message = "两次输入的密码不一致"),
          @ApiResponse(code = 21019, message = "输入的密码不符合规范"),
          @ApiResponse(code = 21022, message = "新旧密码不能一致"),
    })
    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    public RestResponse changePassword(@Validated @RequestBody UserForChangePasswordDTO user) {
        userService.changePassword(user);
        return new RestResponse(UserExceptionEnum.PASSWORD_RESET_COMPLETE.getMsg());
    }

}
